const app = getApp();
const url = require('../../http/url.js');
let that;
Page({

  data: {
    list: [],
    contactFlag: false,
    company: '',
    name: '',
  },

  onLoad: function (options) {
    that = this;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!wx.getStorageSync('isLogin')){
      wx.redirectTo({
        url: '/pages/login/login',
      })
      return
    }
    app.get(url.printList, {}, function (res) {
      if (res.code != 200) {
        wx.showToast({
          title: '服务君走神了哦~~',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      that.setData({
        list: res.data
      })
    })
  },

  /**
   * 输入函数
  */
  printNameInput: function (e) {
    let inputValue = e.detail.value;
    var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
    if (reg.test(inputValue)) {
      that.setData({
        name: inputValue,
      })
    }
  },

  /**
   * 用户列表搜索
   */
  printSearch: function () {
    let name = that.data.name;
    let params = {
      name: name
    };
    app.get(url.printList, {}, function (res) {
      if (res.code != 200) {
        wx.showToast({
          title: '服务君走神了哦~~',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      that.setData({
        list: res.data,
        name: ''
      })
    })
  },

  showConfirm: function (e) {
    wx.showModal({
      title: '提示',
      content: '是否确认打印',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.printFun(e)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  printFun: function (e) {
    let list = that.data.list;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let img = list[index].img;
    let params = {
      id: id,
      img: img
    };
    app.post(url.printFun, params, function (res) {
      if (res.code != 200) {
        wx.showToast({
          title: '服务君走神了哦~~',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      wx.showToast({
        title: '打印成功~~',
        icon: 'none',
        duration: 1000
      });
      list[index].statusAuth = 1;
      that.setData({
        list: list
      });
    })
  },
})